package com.hspedu.newData.dmsxl.回溯.组合;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC77组合 {

    // todo 剪枝优化
    // todo 注意加入到res中的必须新建一个list，不然回溯的过程中会把结果也清空
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> combine(int n, int k) {
        backtracking(n,k,1);
        return res;
    }

    public void backtracking(int n,int k,int startIndex){
        if (path.size() == k){
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            backtracking(n,k,i+1);
            path.remove(path.size() - 1);
        }
    }

    @Test
    public void test() {
        // 测试代码
        List<List<Integer>> combine = combine(4, 2);
        System.out.println("=======");
    }
}
